package com.sgb.goods.view.fragment;

import android.graphics.Color;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.launcher.ARouter;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsFragmentOrderListBinding;
import com.sgb.goods.entity.OrderListEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.GoodsSdk;
import com.sgb.goods.utils.base.fragment.BaseFragment;
import com.sgb.goods.utils.helper.SmartRefreshLayoutHelper;
import com.sgb.goods.utils.module.GoodsModule;
import com.sgb.goods.viewmodel.OrderListViewModel;
import com.sgb.goods.widget.pop.ContractInfoPop;
import com.sgb.goods.widget.pop.LogisticsInfoPop;
import com.sgb.goods.widget.pop.OrderPayInfoPop;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.DisplayUtil;

/**
 * 订单列表
 *
 * @author cyj
 */
public class OrderListFragment extends BaseFragment<GoodsFragmentOrderListBinding, OrderListViewModel>
        implements SmartRefreshLayoutHelper {

    /**
     * 订单类型。1：采购订单；2：销售订单。
     */
    private String orderType = "";
    private String state = "";
    private String processState = "";
    public String orderProcessName = "";
    private OrderPayInfoPop payInfoPop;
    private ContractInfoPop contractInfoPop;
    private LogisticsInfoPop logisticsInfoPop;

    /**
     * 订单列表 构造
     *
     * @param orderType        订单类型。1：采购订单；2：销售订单；
     * @param orderProcessName 订单流转状态名称。如：待接单、履约中等
     * @param state            订单状态（待接单 0 待签收 10 待支付 101 待发货102 已发货103 已完成7 已取消 8 ）,示例值(0)
     * @param processState     审批单查询 固定传1，其他查询都传空,示例值(1)
     */
    public OrderListFragment(String orderType, String orderProcessName, String state, String processState) {
        this.orderType = orderType;
        this.orderProcessName = orderProcessName;
        this.state = state;
        this.processState = processState;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_fragment_order_list;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(OrderListViewModel.class, BR.viewModel);
    }

    @Override
    protected void init() {
        mDataBinding.rlvLayout.rlvView.addItemDecoration(
                new RecyclerCustomDecoration(
                        0, 0, 0, DisplayUtil.dip2px(GoodsSdk.getApplication(), 10),
                        Color.parseColor("#ffF5F5F5")));
    }

    @Override
    public void lazyLoadData() {
        // 获取订单列表
        mViewModel.getOrderListModel(orderType, state, processState, false);
    }

    @Override
    protected void initListener() {
        // 下拉上拉监听
        mDataBinding.rlvLayout.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mViewModel.getOrderListModel(orderType, state, processState, false);
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mViewModel.getOrderListModel(orderType, state, processState, true);
            }
        });
        // 列表item点击事件
        mViewModel.adapter.setOnItemChildClickListener((adapter, view, position) -> {
            OrderListEntity entity = (OrderListEntity) view.getTag();
            int itemViewId = view.getId();
            if (R.id.tv_title == itemViewId) {
                // 反向代理-跳转企业详情(web)页面
                if (entity.orderType == 1) {
                    GoodsModule.getInstance().call1("jumpWebsiteInfoActivity", entity.jumpComNo, 1);
                } else if (entity.orderType == 0) {
                    GoodsModule.getInstance().call1("jumpWebsiteInfoActivity", entity.person, 0);
                }
            } else if (R.id.tv_order_record == itemViewId) {
                // 跳转发票详情页面
                ARouter.getInstance().build(Constants.ARouter.AR_INVOICE_DETAIL)
                        .withString("id", entity.id.toString())
                        .withInt("orderIdentity",entity.orderType.intValue())
                        .navigation();
            } else if (R.id.tv_pay_info == itemViewId) {
                // 支付信息 按钮弹窗
                mViewModel.getListPayDetails(entity, orderType);
            } else if (R.id.tv_deliver_info == itemViewId) {
                // 物流信息 按钮弹窗
                mViewModel.getDeliverByOrderId(entity, orderType);
            } else if (R.id.tv_contract_info == itemViewId) {
                // 合同信息 按钮弹窗
                mViewModel.getContractDetailsByOrderId(entity, orderType);
            } else if (R.id.ll_goods_desc == itemViewId) {
                // 跳转采购订单详情或者销售订单详情页面
                ARouter.getInstance().build("1".equals(orderType) ? Constants.ARouter.AR_PURCHASE_ORDER_DETAILS : Constants.ARouter.AR_SELL_ORDER_DETAILS)
                        .withString("orderId", entity.id + "")
                        .navigation();
            }
        });
    }

    @Override
    protected void initObserve() {
        // 订单列表数据填充后UI刷新
        mViewModel.orderList.observe(this, orderList -> {
            // SmartRefreshLayout 代理UI处理
            refreshUi(mDataBinding.rlvLayout.refreshLayout, mViewModel.adapter, orderList);
        });
        // 支付信息列表弹窗
        mViewModel.orderPayDetail.observe(this, orderPayInfoEntity -> {
            if (null == payInfoPop) {
                payInfoPop = new OrderPayInfoPop(getContext());
                payInfoPop.setBgView(mDataBinding.grayLayout);
            }
            payInfoPop.showPop(mDataBinding.grayLayout, orderPayInfoEntity, orderType);
        });
        // 合同信息列表弹窗
        mViewModel.contractInfo.observe(this, contractInfo -> {
            if (null == contractInfoPop) {
                contractInfoPop = new ContractInfoPop(getContext());
                contractInfoPop.setBgView(mDataBinding.grayLayout);
            }
            contractInfoPop.showPop(mDataBinding.grayLayout, contractInfo, orderType);
        });
        // 物流信息列表弹窗
        mViewModel.logisticsDetail.observe(this, logisticsDetail -> {
            if (null == logisticsInfoPop) {
                logisticsInfoPop = new LogisticsInfoPop(getContext());
                logisticsInfoPop.setBgView(mDataBinding.grayLayout);
            }
            logisticsInfoPop.showPop(mDataBinding.grayLayout, logisticsDetail, orderType);
        });
    }
}
